import rtoml
import json
from utils import get_book_url, part_str_in, map_keyword_group, map_exact_group


ignore_groups = ['你就像人间的四月天', '⛱️免费书源', 'po', '搜索链接规则为空', '天龙', 'API', 'api·凡', '源仓库',
                 '水幽临渊0905', '合集0905', '[小说源]', 'JSON', "兔兔兔", "⛎JJ城", "笔趣阁0905", "💠 综合",
                 "发现", "📖 小说", "网页源", "自写修复0909", "自制", "一般", "⭐️小说", "1", "c", "♑qd书",
                 "✉️  网络文学", "🪁轻小说", "天域战歌", "小说", "✯·常用", "♾️备选", "🔥 书耽", "🥈补充"
                 "Namo", "b", '🥈补充', 'Namo', '2', '🗂 聚合', '🐑 下载', '❄️自用', '📖轻小说', '🗨️ 轻文',
                 '✉️  日轻文学', '⌨自制', '🔖 其他', '✉️  其他文学', '♾️', '小白自制', '⭐常用', '💎 常用',
                 '轻小说', '📝 小众', '📖小说', '原仓库', '漫画', '暂不可用', '$$$', '🇯🇵轻小说', '破冰&一程0902',
                 '🌐 网站']


def modify_group_name(group):
    if 'fox' in group:
        pass
    elif '漫画' in group:
        group = '漫画'
    elif '有声' in group:
        group = '有声'
    elif part_str_in(group, ['🔞', '肉文', '18', 'adult', '绅士']):
        group = '18'
    elif '女频' in group:
        group = '女频'
    elif '正版' in group:
        group = '正版'
    elif '出版' in group:
        group = '出版'
    elif part_str_in(group, ['校验', '检验', '精选', '精品', '正常', '普通', '有效', '网文', '验证', '优质',
                             '书源', '免费', '笔趣', '规则', '失效', '效验']):
        group = ''
    elif group in ignore_groups:
        group = ''
    return group


def get_group_books(src_books):
    group_books = {}
    for book in src_books:
        if 'bookSourceGroup' in book and book['bookSourceGroup']:
            groups = book['bookSourceGroup'].split(',')
            book_name = book['bookSourceName']
            book_url = get_book_url(book)
            for group in groups:
                group = modify_group_name(group)
                if not group:
                    continue

                if group not in group_books:
                    group_books[group] = {}
                group_books[group][book_name] = book_url
    print(group_books.keys())
    return group_books


def remove_ignore_groups(src_books):
    for book in src_books:
        if book.get('bookSourceGroup'):
            groups = book['bookSourceGroup'].split(',')
            new_groups = []
            for group in groups:
                group = modify_group_name(group)
                if group:
                    new_groups.append(group)
            book['bookSourceGroup'] = ','.join(new_groups)


def main():
    src_path = './output/url_sort.json'
    dst_toml_path = './output/book_groups.toml'
    dst_json_path = './output/book_new_groups.json'

    with open(src_path, 'r', encoding='utf-8') as f:
        src_books = json.load(f)

    map_keyword_group(src_books)
    map_exact_group(src_books)

    book_groups = get_group_books(src_books)
    # print(book_groups['18'].keys())

    toml_text = rtoml.dumps(book_groups)
    with open(dst_toml_path, 'w', encoding='utf-8') as f:
        f.write(toml_text)

    remove_ignore_groups(src_books)
    with open(dst_json_path, 'w', encoding='utf-8') as f:
        json_str = json.dumps(src_books)
        f.write(json_str)


if __name__ == '__main__':
    main()
